<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Player_model extends CI_Model {

	function get_song_meta($fileTitle)
	{
		$song = explode('-',$fileTitle);		
		
		$where = array('strArtist'=>trim(strtoupper($song[0])),'strSongTitle'=>trim(strtoupper($song[1])));
		
		
		$ret = $this->db
					->where($where)					
					->get('tb_mas_songs')
					->result_array();
					
		return current($ret);
	}
	
	function get_song_by_id($song_id)
	{
		$where = array('intID'=>$song_id);
		$ret = $this->db
					->where($where)					
					->get('tb_mas_songs')
					->result_array();
					
		return current($ret);
	}
	
	function get_album_art($albumid)
	{
		$album_art = $this->db
						  ->get_where('tb_mas_albums',array('intID'=>$albumid))
						  ->first_row()
						  ->strPicture;
		
		return $album_art;
	}
	
	function update_last_played($id)
	{
		$date = date('Y-m-d H:i:s');
		$this->db
			 ->where('intID',$id)
			 ->update('tb_mas_songs',array('dteLastPlayed'=>$date));
	}
	function get_most_requested($community_id)
    {  
        return $this->db
                    ->select('*')
                    ->from('tb_mas_requests')
                    ->where('fk-tb_mas_community',$community_id)
                    ->order_by('intRequests','desc')
                    ->limit(10)
                    ->group_by(array("strSongTitle", "strArtist"))
                    ->get()
                    ->result_array();
    }
	function get_last_played($community)
	{	
		 return $this->db
					 ->where('fk-tb_mas_community',$community)
					 ->order_by('dteLastPlayed','desc')
					 ->limit(10,1)					 
					 ->get('tb_mas_songs')		     
					 ->result_array();
		
	}
    
    function get_all_videos()
    {
        return $this->db
                    ->order_by('dteStart','desc')
                    ->get('tb_mas_videos')
                    ->result_array();
    }
    
    function process_request($song,$community_id)
    {
        $song = explode('-',$song);
        $data['strArtist'] = $song[0];
        $data['strSongTitle'] = $song[1];
        $data['fk-tb_mas_users'] = $this->session->userdata('intID');
        $data['fk-tb_mas_community'] = $community_id;
        $diff = -1;
        
        $existing = $this->db
                        ->select('*')
                        ->from('tb_mas_requests')
                        ->where($data)
                        ->order_by('dteDateRequested','desc')
                        ->get()
                        ->result_array();
        
        
        if(!empty($existing))
        {
            $get = current($existing);
            $now = time(); // or your date as well
            $your_date = strtotime($get['dteDateRequested']);
            $datediff = abs($now - $your_date);
            $diff = floor($datediff/(60*60*24));
        }
        
        if($diff == 0)
        {
            return 0;
        }
        else if($diff == -1)
        {

            $data['dteDateRequested'] = date('Y-m-d H:i:s');
            $data['intRequests'] = 1;
            $this->db->insert('tb_mas_requests',$data);
            return 1;
        }
        else
        {
            $data['dteDateRequested'] = date('Y-m-d H:i:s');
            $data['intRequests'] = $get['intRequests']+1; 
            $this->db->insert('tb_mas_requests',$data);
            return 2;
        }
    }

}